package com.code4hire.desktop.converter;

import java.util.List;

import com.code4hire.desktop.converter.model.Authentication;
import com.code4hire.desktop.converter.model.Profile;
import com.code4hire.desktop.converter.model.ProfileVersion;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Rest {

    private static final String RESPONSE_TYPE = "application/xml";
    private static final String SERVER = "http://localhost:8080/video/";

    public static Authentication login(String username, String password) {
        try {

            Client client = Client.create();

            WebResource webResource = client.resource(SERVER
                    + String.format("login?username=%s&password=%s", username, password));

            ClientResponse response = webResource.accept(RESPONSE_TYPE).get(ClientResponse.class);

            if (response.getStatus() == 200) {
                String output = response.getEntity(String.class);
                XStream xStream = new XStream(new DomDriver());
                xStream.alias("Authentication", Authentication.class);
                return (Authentication) xStream.fromXML(output);
            } else {
                // TODO log
                return null;
            }

        } catch (Exception e) {
            // TODO log
            e.printStackTrace();
            return null;
        }
    }

    public static ProfileVersion checkNewVersion() {
        try {

            Client client = Client.create();

            WebResource webResource = client.resource(SERVER + "version");

            ClientResponse response = webResource.accept(RESPONSE_TYPE).get(ClientResponse.class);

            if (response.getStatus() == 200) {
                String output = response.getEntity(String.class);
                XStream xStream = new XStream(new DomDriver());
                xStream.alias("ProfileVersion", ProfileVersion.class);
                return (ProfileVersion) xStream.fromXML(output);
            } else {
                // TODO log
                return null;
            }
        } catch (Exception e) {
            // TODO log
            e.printStackTrace();
            return null;
        }
    }

    public static List<Profile> update() {
        try {

            Client client = Client.create();

            WebResource webResource = client.resource(SERVER + "update");

            ClientResponse response = webResource.accept(RESPONSE_TYPE).get(ClientResponse.class);

            if (response.getStatus() == 200) {
                String output = response.getEntity(String.class);
                System.out.println(output);
                XStream xStream = new XStream(new DomDriver());
                xStream.alias("Profile", Profile.class);
                return (List<Profile>) xStream.fromXML(output);
            } else {
                // TODO log
                return null;
            }
        } catch (Exception e) {
            // TODO log
            e.printStackTrace();
            return null;
        }
    }
}
